package com.zty.utils;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

/**
 * @author tyzhang19
 * @Description 读取xml中的配置.
 * @date 2021-08-02 18:30
 */
public class ReadXML {

    public static Object getObject(){
        try {
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document document = builder.parse(new File("src/main/resources/config.xml"));
            NodeList nodeList = document.getElementsByTagName("className");
            Node classNode = nodeList.item(0).getFirstChild();
            String cName = "com.zty.factory_method_pattern.factory." + classNode.getNodeValue();
            System.out.println("新类名：" + cName);
            Class<?> c = Class.forName(cName);
            return c.newInstance();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

